/**
 * @(#)CreateAccountActivity.java        1.0 2/22/12
 * 
 * Copyright (c) 2012 public static droid main
 * Georgia Institute of Technology, Atlanta, GA
 * All rights reserved.
 */

package edu.gatech.oad.publicstaticdroidmain.taskmanager.ui;

import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import edu.gatech.oad.publicstaticdroidmain.taskmanager.model.InputValidator;
import edu.gatech.oad.publicstaticdroidmain.taskmanager.model.LoginManager;

/**
 * An Android activity that allows a user to create an account.  
 * Contains a form for entering user information.
 * 
 * @version 1.0 Feb 22 2012
 * @author Raheel Merali
 */
public class CreateAccountActivity extends AbstractTaskManagerActivity {

    private OnClickListener buttonListener;

    private TextView nameTextField;
    private TextView emailAddressTextField;
    private TextView usernameTextField;
    private TextView passwordTextField;
    private Button createAccountButton;

    private LoginManager loginManager;

    /**
     * Sets up the layout and captures the form elements into Java objects.
     * 
     * @param savedInstanceState saved data to reload
     */
    @SuppressWarnings("static-access")
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.create_account);

	nameTextField = (TextView) findViewById(R.id.createAccountNameTextField);
	emailAddressTextField = (TextView) findViewById(R.id.createAccountEmailTextField);
	usernameTextField = (TextView) findViewById(R.id.createAccountUsernameTextField);
	passwordTextField = (TextView) findViewById(R.id.createAccountPasswordTextField);
	createAccountButton = (Button) findViewById(R.id.createAccountCreateButton);

	buttonListener = new CreateAccountActivityButtonListener();
	createAccountButton.setOnClickListener(buttonListener);

	loginManager = ((TaskManagerApplication)getApplication()).getLoginManager();
    }

    /**
     * Returns false so that a menu will not be displayed when the user
     * clicks the menu button.
     */
    public boolean onCreateOptionsMenu(Menu menu) {
	return false;
    }

    /**
     * Determines if user inputs are correctly formatted and if an account with
     * these inputs can be successfully created.  If any of the inputs are invalid,
     * an error toast is displayed telling the user which input is incorrect.
     * 
     * @param username
     * @param password
     * @param emailAddress
     * @param realName
     * @return true if the inputs are correctly formatted and if it is possible to
     * 		create an account with these inputs; false otherwise
     */
    private boolean validateInputsAndShowErrorMessages(String username, String password, String emailAddress, String realName) {
	if (!InputValidator.isValidUsername(username)) {
	    showErrorToast(getString(R.string.create_account_invalid_username_message));
	    return false;
	}

	if (!InputValidator.isValidPassword(password)) {
	    showErrorToast(getString(R.string.create_account_invalid_password_message));
	    return false;
	}

	if (!InputValidator.isValidEmailAddress(emailAddress)) {
	    showErrorToast(getString(R.string.create_account_invalid_email_address_message));
	    return false;
	}

	if (!InputValidator.isValidRealName(realName)) {
	    showErrorToast(getString(R.string.create_account_invalid_real_name_message));
	    return false;
	}

	if (loginManager.userExists(username)) {
	    showErrorToast(getString(R.string.create_account_username_taken_message));
	    return false;
	}

	return true;
    }

    /**
     * Called when the "Create" button is pressed.  Handles the actual account
     * creation in the database.
     */
    private class CreateAccountActivityButtonListener implements OnClickListener {
	public void onClick(View view)
	{
	    String username = usernameTextField.getText().toString().trim();
	    String password = passwordTextField.getText().toString().trim();
	    String emailAddress = emailAddressTextField.getText().toString().trim();
	    String realName = nameTextField.getText().toString().trim();

	    if (!validateInputsAndShowErrorMessages(username, password, emailAddress, realName)) {
		return;
	    }

	    loginManager.createUser(username, password, realName, emailAddress);	    	
	    showSuccessToast(getString(R.string.create_account_success_message));
	    startUIActivity(getString(R.string.activity_login));

	} //end onClick method
    } //end CreateAccountActivityButtonListener

} //end CreateAccountActivity
